<script setup>
import {
  Edit,
  Delete,
  Close
} from '@element-plus/icons-vue'


import { ref } from 'vue'





const approves = ref([
  {
    "id": 0,
    "contentValue": "待审批",
  },
  {
    "id": 1,
    "contentValue": "审批完成",
  }
])





//用户搜索时选中的分类id
const subject = ref('')



//用户搜索时选中的发布状态
const approve = ref('')




const dataList = ref([


])



//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(15)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  queryDataList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  queryDataList()
}


import { questionAddService, questionListService, questionDeleteService, questionUpdateService, questionPublishService } from '@/api/question.js'



import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({

})


//导入token
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadSuccess = (result) => {
  dataModel.value.imgUrl = result.data;
  console.log(result.data);
}

const uploadAudioSuccess = (result) => {
  dataModel.value.audioUrl = result.data;
  console.log(result.data);
}


//添加文章
import { ElMessage } from 'element-plus'



//获取文章列表数据
const queryDataList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    approveStatus: approve.value !=='' ? approve.value : null
  }

  let result = await treeHoleListService(params);

  //渲染视图
  total.value = result.data.total;
  dataList.value = result.data.records;

}


queryDataList();



//删除分类
import { ElMessageBox } from 'element-plus'
import { useFavicon } from '@vueuse/core';
import {treeHoleApproveService, treeHoleDelService, treeHoleListService} from "@/api/treeHole.js";
const delData = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该树洞信息吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await treeHoleDelService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        queryDataList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}


//定义变量,控制标题的展示
const title = ref('')


//清空模型的数据




const selected = (row, index) => {
  return dataList.value[index].approveStatus == 0
}

let select = []
const publish = async () => {
  console.log(select)
  if (select.length === 0) {
    ElMessage.error("请选择要审批的树洞")
    return
  }
  let ids = []
  select.forEach(element => {
    ids.push(element.id)
  });
  let params = {
    ids: ids
  }
  let result = await treeHoleApproveService(params);

  queryDataList();

}
const handleSelectionChange = (val) => {
  select = val;
}

const showState = ref(0)
const changeQuestionType = (val) => {
  if (val == 0) {
    showState.value = 1;
  } else {
    showState.value = 0;
  }
}


</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>用户树洞</span>

      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="类型：">
        <el-select placeholder="请选择" v-model="approve">
          <el-option v-for="c in approves" :key="c.id" :label="c.contentValue" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="queryDataList">搜索</el-button>
        <el-button @click="subject = ''; state = ''">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form inline>
      <el-button type="primary" @click="publish">批量审批</el-button>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" :selectable="selected" />
      <el-table-column label="序号" prop="id" width="100"></el-table-column>
      <el-table-column label="用户" prop="appUser.account" width="200"></el-table-column>
      <el-table-column label="标题" prop="title" width="200"></el-table-column>
      <el-table-column label="文本" prop="text" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="审批状态" width="100px">
        <template v-slot="scope">
          <span v-if="scope.row.approveStatus == 0">审批中</span>
          <span v-else>审批完成</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>
      <el-table-column label="操作" width="150px">
        <template #default="{ row }">
          <el-button :icon="Delete" circle plain type="danger" @click="delData(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>


    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />





  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>